home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11052 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  97 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news.sprintlink.net!mv!usenet
  3. From: ENGR@GSSI.MV.COM (Michael Furman)
  4. Subject: Re: "Deep" Destructor Query
  5. Message-ID: <Do5roH.Jsx@mv.mv.com>
  6. Mime-Version: 1.0
  7. Content-Type: Text/Plain; charset=US-ASCII
  8. Organization: GSSI
  9. Date: Tue, 12 Mar 1996 14:17:05 GMT
  10. References: <826257180snz@pbutler.demon.co.uk>
  11. X-Newsreader: WinVN 0.99.7
  12. X-Nntp-Posting-Host: gssi.mv.com
  13.  
  14. In article <826257180snz@pbutler.demon.co.uk>, Peter@pbutler.demon.co.uk 
  15. says...
  16. > .... snip
  17. >struct point
  18. >{
  19. >int x, y, z;
  20. >};
  21. >
  22. >Class Shape
  23. >{
  24. >private:
  25. >point *p;  //dynamic array of points
  26. >//...etc
  27. >};
  28. >
  29. >Class Object
  30. >{
  31. >private:
  32. >int numshapes;  //number of data elements
  33. >shape *shapes;  //dynamic array of shapes
  34. >//etc...
  35. >};
  36. >
  37. >Now, this all works quite well, apart from when it comes to destructor
  38. >functions.  For shape I have:
  39. >
  40. >Shape::~Shape()
  41. >{
  42. >if (p)
  43. >        delete [] p;
  44. >}
  45. >
  46. >which works ok.  But when I wrote a destructor for Object, like this:
  47. >
  48. >Object::~Object()
  49. >{
  50. >for (int i = 0; i < numshapes; i++)
  51. >        shapes[i].~shape();  //"Deep" destructor, deletes underlying arrays.
  52. >
  53. >delete [] shapes;  //delete the array of pointers
  54. >}
  55. >
  56. >The program causes a General Protection Fault (guess which OS I'm using and 
  57. >win a cookie).  It fails when it tries to delete the array of null pointers
  58. >pointed to by shape.  It works when I leave the "delete [] shapes;" line 
  59. out.
  60. >
  61. >Well, I have two questions:
  62. >
  63. >1) If I simply "delete [] shapes" with no loop to delete the arrays 
  64. underneath,
  65. >   will the ~shape() destructor be automatically called, thus deleting the
  66. >   shape objects correctly?  I wouldn't think so, but it would be good to be
  67. >   corrected.
  68.  
  69. Yes (you can check it yourself: just insert "printf(...)" in the destructor,
  70. run program and look at the results).
  71.  
  72. >
  73. >2) If I delete each shape with a loop (as above), does this mean that shapes
  74. >   pointer points to nothing?  I would have thought that it would point to
  75. >   an array of null pointers.
  76.  
  77. After you explicitly call destuctor, you have just raw memory - not array
  78. of objects. You can't using "delete" after that (but you can call 
  79. "operator delete" to free memory.
  80.  
  81.  
  82. >
  83. >Thanks for any comments/opinions/advice.
  84. >
  85. >-- 
  86. >Peter Hugh Butler
  87.  
  88. -- 
  89. <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
  90. ---------------------------------------------------------------
  91. Michael Furman,                       (603)893-1109
  92. Geophysical Survey Systems, Inc.  fax:(603)889-3984
  93. 13 Klein Drive - P.O. Box 97          engr@gssi.mv.com 
  94. North Salem, NH 03073-0097            71543.1334@compuserve.com
  95. ---------------------------------------------------------------
  96.  
  97.